package com.gz.service.impl;

import com.gz.common.RedisConst;
import com.gz.entity.Vo.CartItemVo;
import com.gz.entity.Vo.CartVo;
import com.gz.mapper.BookMapper;
import com.gz.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private RedisTemplate<String, CartVo> redisTemplate;

    @Autowired
    private BookMapper bookMapper;

    /**
     * 获取用户的购物车
     *
     * @param userId
     */
    @Override
    public CartVo getCart(Long userId) {
        //从Redis中获取用户的购物车
        ValueOperations<String, CartVo> ops = redisTemplate.opsForValue();
        CartVo cart = ops.get(RedisConst.USER_CART_PREFIX + ":" + userId);

        //如果Redis中没有，创建一个新的购物车
        if (cart == null) {
            cart = new CartVo();
            //添加用户的ID到购物车
            cart.setUserId(userId);
        }
        //将数据存入Redis
        ops.set(RedisConst.USER_CART_PREFIX + ":" + userId, cart);
        return cart;
    }

    /**
     * 添加一件商品到购物车
     *
     */
    @Override
    public CartVo addCartItem(CartItemVo cartItemVo) {
        CartVo cart = this.getCart(cartItemVo.getUserId());
        System.out.println("添加之前："+cart);
        Map<Long, CartItemVo> items = cart.getItems();
        boolean b = items.containsKey(Long.valueOf(cartItemVo.getBookId()));
        if (b) {
            CartItemVo bookItem = items.get(cartItemVo.getBookId());
            bookItem.setQuantity(bookItem.getQuantity() + cartItemVo.getQuantity());
            bookItem.setSum(bookItem.getSum() + cartItemVo.getSum());
            items.put(bookItem.getBookId(), bookItem);
        }else {
            items.put(cartItemVo.getBookId(), cartItemVo);
        }

        cart.updateAmountAndTotalPrice();

        ValueOperations ops = redisTemplate.opsForValue();
        ops.set(RedisConst.USER_CART_PREFIX + ":" + cartItemVo.getUserId(), cart);
        System.out.println("添加之后："+cart);

        return cart;
    }

    /**
     * 删除购物车项
     *
     * @param userId
     * @param bookId
     */
    @Override
    public CartVo removeCartItem(Long userId, Long bookId) {
        return null;
    }

    @Override
    public void updateCart(Long bookId, Integer quantity, Long id) {
        CartVo cart = getCart(id);
        CartItemVo cartItemVo = cart.getItems().get(bookId);
        cartItemVo.setQuantity(quantity);
        cartItemVo.setSum(cartItemVo.getPriceToQuantitySum());

        cart.getItems().put(bookId, cartItemVo);
        cart.updateAmountAndTotalPrice();
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set(RedisConst.USER_CART_PREFIX + ":" + id, cart);
    }

    @Override
    public void deleteCartItem(Long bookId, Long id) {
        CartVo cart = getCart(id);
        Map<Long, CartItemVo> items = cart.getItems();
        items.remove(bookId);
        cart.setItems(items);
        cart.updateAmountAndTotalPrice();
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set(RedisConst.USER_CART_PREFIX + ":" + id, cart);
    }

    @Override
    public CartVo clearCart(Long userId) {
        CartVo cart = getCart(userId);
        Map<Long, CartItemVo> items = cart.getItems();
        items.clear();
        cart.setItems(items);
        cart.updateAmountAndTotalPrice();
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set(RedisConst.USER_CART_PREFIX + ":" + userId, cart);
        return cart;
    }

    @Override
    public void deleteCart(Long userId) {
        this.clearCart(userId);
        redisTemplate.delete(RedisConst.USER_CART_PREFIX + ":" + userId);
    }
}
